/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ile.VISTA;

import ile.DAO.HerramientaDAO;
import ile.DAO.MarcaDAO;
import ile.DAO.OrdenTrabajoDAO;
import ile.DAO.PedidoDAO;
import ile.DAO.UsuarioDAO;
import ile.ModeloTabla.ListadePedidosModelTable;
import ile.Modelo.Herramienta;
import ile.Modelo.Marca;
import ile.Modelo.OrdenTrabajo;
import ile.Modelo.Pedido;
import ile.Modelo.Usuario;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author 
 */
public class PedidoH extends javax.swing.JDialog {
    ListadePedidosModelTable lpmt;
    List<Pedido> pedidos;
    
    Usuario usuario;
    List<OrdenTrabajo> ordenTrabajos;
    
    Herramienta herramienta= null;

    /**
     * Creates new form PedidoHerramientas
     */
    public PedidoH(java.awt.Frame parent, boolean modal, Usuario u) {
        super(parent, modal);
        lpmt=new ListadePedidosModelTable();
        pedidos=new ArrayList<Pedido>();
        usuario=u;
        ordenTrabajos=usuario.getOrdenTrabajos();
        initComponents();
        
        lblUsuario.setText(usuario.getNombre());
       // setMaximumSize(Integer.MAX_VALUE);
       //setSize(new DimensionUIResource(Integer.MAX_VALUE,Integer.MAX_VALUE));
   
        setLocationRelativeTo(null);
    }

   
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jPanel1 = new javax.swing.JPanel();
        lblUsuario = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listaPedidosH = new javax.swing.JTable();
        btnElimnarPH = new javax.swing.JButton();
        OTSh = new javax.swing.JComboBox();
        lblOT1 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tablaHerramientas = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        txtNomHerramienta = new javax.swing.JTextField();
        btnBuscarH = new javax.swing.JButton();
        btnOkH = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        lblImagen = new javax.swing.JLabel();
        btnCancelar = new javax.swing.JButton();
        btnAceptarH = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(126, 166, 189));
        jPanel1.setAutoscrolls(true);

        jPanel2.setBackground(new java.awt.Color(126, 166, 189));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(java.awt.Color.white, 2, true), "Lista de Pedidos de Herramientas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Serif", 1, 16))); // NOI18N

        listaPedidosH.setBackground(new java.awt.Color(225, 254, 210));
        listaPedidosH.setModel(lpmt);
        jScrollPane2.setViewportView(listaPedidosH);

        btnElimnarPH.setFont(new java.awt.Font("DejaVu Serif", 1, 15)); // NOI18N
        btnElimnarPH.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Borrar.png"))); // NOI18N
        btnElimnarPH.setText("Eliminar");
        btnElimnarPH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnElimnarPHActionPerformed(evt);
            }
        });

        OTSh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${ordenTrabajos}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, OTSh);
        bindingGroup.addBinding(jComboBoxBinding);

        lblOT1.setFont(new java.awt.Font("DejaVu Serif", 1, 23)); // NOI18N
        lblOT1.setForeground(java.awt.Color.black);
        lblOT1.setText("Numero OT:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2)
                .addGap(15, 15, 15)
                .addComponent(btnElimnarPH, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(221, 221, 221)
                .addComponent(OTSh, 0, 350, Short.MAX_VALUE)
                .addGap(445, 445, 445))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(lblOT1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(36, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblOT1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(OTSh, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnElimnarPH, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel4.setBackground(new java.awt.Color(126, 166, 189));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(java.awt.Color.white, 2, true), "Lista de Herramientas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Serif", 1, 16))); // NOI18N

        tablaHerramientas.setBackground(new java.awt.Color(225, 254, 210));
        tablaHerramientas.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        tablaHerramientas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tablaHerramientas.setGridColor(new java.awt.Color(5, 5, 5));
        tablaHerramientas.setSelectionBackground(new java.awt.Color(151, 199, 234));
        tablaHerramientas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaHerramientasMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tablaHerramientas);

        jLabel3.setFont(new java.awt.Font("DejaVu Serif", 1, 23)); // NOI18N
        jLabel3.setForeground(java.awt.Color.black);
        jLabel3.setText("Nombre:");

        txtNomHerramienta.setFont(new java.awt.Font("DejaVu Serif", 0, 22)); // NOI18N
        txtNomHerramienta.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtNomHerramientaMouseClicked(evt);
            }
        });
        txtNomHerramienta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNomHerramientaKeyTyped(evt);
            }
        });

        btnBuscarH.setFont(new java.awt.Font("DejaVu Serif", 1, 15)); // NOI18N
        btnBuscarH.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/view.png"))); // NOI18N
        btnBuscarH.setText("Buscar");
        btnBuscarH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarHActionPerformed(evt);
            }
        });

        btnOkH.setFont(new java.awt.Font("DejaVu Serif", 1, 15)); // NOI18N
        btnOkH.setText("Ok");
        btnOkH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkHActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("DejaVu Serif", 1, 20)); // NOI18N
        jLabel4.setText("Gráfica");

        lblImagen.setFont(new java.awt.Font("DejaVu Serif", 1, 23)); // NOI18N
        lblImagen.setForeground(java.awt.Color.black);
        lblImagen.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNomHerramienta)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBuscarH, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 806, Short.MAX_VALUE))
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(btnOkH, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(jLabel4))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtNomHerramienta, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnBuscarH, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnOkH, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        btnCancelar.setFont(new java.awt.Font("DejaVu Serif", 1, 15)); // NOI18N
        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/cancelar.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnAceptarH.setFont(new java.awt.Font("DejaVu Serif", 1, 15)); // NOI18N
        btnAceptarH.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/ok.png"))); // NOI18N
        btnAceptarH.setText("Aceptar");
        btnAceptarH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarHActionPerformed(evt);
            }
        });

        jLabel1.setText("Usuario:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnAceptarH, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 344, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 343, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnAceptarH, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(22, 22, 22)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(403, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtNomHerramientaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtNomHerramientaMouseClicked
        // TODO add your handling code here:
        
        new TecladoVirtual(null, true, txtNomHerramienta, tablaHerramientas, "PedidosH").setVisible(true);
    }//GEN-LAST:event_txtNomHerramientaMouseClicked

    private void btnOkHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkHActionPerformed
        // TODO add your handling code here:
        DefaultTableModel modelo=(DefaultTableModel) tablaHerramientas.getModel();
        for (int i = 0; i < modelo.getRowCount(); i++) {
           String cadena=String.valueOf(tablaHerramientas.getValueAt(i, 6));
            Boolean bandera= Boolean.valueOf(cadena);
                 if(bandera==true){     

                     Marca marca=marcas.get(i);
                     Pedido pedido=new Pedido();

                      try {
                           String cantidad = String.valueOf(tablaHerramientas.getValueAt(i, 5));
                            int cant= Integer.valueOf(cantidad);
                            pedido.setCantidad(cant);
                            Date date=new Date();
                            DateFormat dateFormat= new SimpleDateFormat("dd/MM/yyyy");                   
                            pedido.setFecha(dateFormat.format(date));
                           
                            try {
          
                             pedido.setMarca(marca);
                             pedidos.add(pedido);
  
                            
                            } catch (Exception ex) {
                            Logger.getLogger(PedidoH.class.getName()).log(Level.SEVERE, null, ex);
                            }                           
                     } catch (Exception e) {
                         JOptionPane.showMessageDialog(null, "Existen Campos Vacíos");
                     }
                  
                 }else{
                    String cad=String.valueOf(tablaHerramientas.getValueAt(i, 5));
                    if(bandera==false && !cad.equals("")){
                        JOptionPane.showMessageDialog(null, "Existen Campos Vacíos");
                        break;
                    }
                   
                 }
        }

        lpmt= (ListadePedidosModelTable) listaPedidosH.getModel();
        lpmt.setFilas(pedidos);

        for (int i = 0; i < tablaHerramientas.getRowCount(); i++) {
           tablaHerramientas.setValueAt(false,i, 6);
           tablaHerramientas.setValueAt("",i, 5);

        }
        
     // lpmt.setPedido(null);
    }//GEN-LAST:event_btnOkHActionPerformed

    private void btnElimnarPHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnElimnarPHActionPerformed
        // TODO add your handling code here:
        ListadePedidosModelTable pedidosMT = (ListadePedidosModelTable) listaPedidosH.getModel();
        Pedido pedidoSeleccionado = pedidosMT.getPedido();
        List<Pedido> listaPedidos = pedidosMT.getFilas();
        if (pedidoSeleccionado != null) {
            int rta = JOptionPane.showConfirmDialog(this, "Desea realmente eliminar el Pedido? "
                    + pedidoSeleccionado.getMarca().getHerramienta(), "Eliminar", JOptionPane.YES_NO_OPTION);

            if (rta == JOptionPane.YES_OPTION) {
                listaPedidos.remove(pedidoSeleccionado);
                pedidosMT.setFilas(listaPedidos);
                JOptionPane.showMessageDialog(this, "Pedido eliminado", "Información", JOptionPane.INFORMATION_MESSAGE);

            }

        }else{
            JOptionPane.showMessageDialog(this, "Debe seleccionar un Pedido de la tabla", "Error", JOptionPane.ERROR_MESSAGE);
        }
 
    }//GEN-LAST:event_btnElimnarPHActionPerformed

    private void btnBuscarHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarHActionPerformed
        // TODO add your handling code here:
        cargarTablaHerramientas();
    }//GEN-LAST:event_btnBuscarHActionPerformed

    private void txtNomHerramientaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNomHerramientaKeyTyped
        // TODO add your handling code here:
        cargarTablaHerramientas();
    }//GEN-LAST:event_txtNomHerramientaKeyTyped

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAceptarHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarHActionPerformed
        OrdenTrabajo otSeleccionada=(OrdenTrabajo) OTSh.getSelectedItem();
        
        System.out.println(otSeleccionada);
       
        ListadePedidosModelTable listPedidoMT= (ListadePedidosModelTable) listaPedidosH.getModel();
        Pedido pedidoSeleccionado= listPedidoMT.getPedido();
        if(pedidoSeleccionado!= null&& otSeleccionada!=null ){

        for (int i = 0; i < pedidos.size(); i++) {
                     Pedido pedido= pedidos.get(i);
               
                     Marca marca=pedido.getMarca();
                     PedidoDAO pedidoDAO= new PedidoDAO(pedido);
                            marca.agregar(pedido);
                       otSeleccionada.agregarOTH(pedido);
                       usuario.agregarP(pedido);
                       OrdenTrabajoDAO dAO= new OrdenTrabajoDAO(otSeleccionada);
                                             

                       
                       UsuarioDAO udao=new UsuarioDAO(usuario);
                        
                            MarcaDAO marcaDAO=new MarcaDAO(marca);
            try {
                pedidoDAO.persist();
                marcaDAO.update();
                dAO.update();
                udao.update();
                dispose();
            } catch (Exception ex) {
                Logger.getLogger(PedidoH.class.getName()).log(Level.SEVERE, null, ex);
            }

                 
        }
        JOptionPane.showMessageDialog(null, "Pedido almacenado satisfactoriamente");
        dispose();
        }else{
           
           JOptionPane.showMessageDialog(this, "No ha realizado ningun pedido", "Error", JOptionPane.ERROR_MESSAGE);
           dispose();
        }

    }//GEN-LAST:event_btnAceptarHActionPerformed

    private void tablaHerramientasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaHerramientasMouseClicked
//   if (tablaHerramientas.getRowCount() > 0) {
//            int aux = tablaHerramientas.getSelectedRow();
//            if (aux != -1) {
//                herramienta = (Herramienta) herramientas.get(aux);
//                CargarImagenes ci = new CargarImagenes();
//                try {
//                    Image im = ci.getfoto(String.valueOf(herramienta.getId()));
//                    lblImagen.setIcon(new ImageIcon(im));
//                } catch (SQLException ex) {
//                    Logger.getLogger(PedidosMateriales.class.getName()).log(Level.SEVERE, null, ex);
//                }
//            } else {
//                JOptionPane.showMessageDialog(this, "Seleccione una herramienta ...", "Herramienta", JOptionPane.WARNING_MESSAGE);
//            }
//        } else {
//            JOptionPane.showMessageDialog(this, "No existen elementos en la tabla ...", "Herramientas", JOptionPane.WARNING_MESSAGE);
//        }
//        
//        tablaHerramientas=null; 
    }//GEN-LAST:event_tablaHerramientasMouseClicked

     List<Marca> marcas;
    List<Herramienta> herramientas=null;
        public void cargarTablaHerramientas(){
         HerramientaDAO herramientaDAO= new HerramientaDAO(null);
       herramientas=herramientaDAO.buscarPorNombre(txtNomHerramienta.getText());
        marcas= new ArrayList<Marca>();
        for (int i = 0; i <herramientas.size(); i++) {
            marcas.addAll(herramientas.get(i).getMarcas());

        }
//        
        DefaultTableModel TableModel = new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                if(columnIndex==5 || columnIndex==6){
                return true;
                }else{
                return false;
                }

             }

            @Override
            public Class getColumnClass(int columna){
            if(columna==5){
            return Integer.class;
            }if(columna==6){
                return Boolean.class;
            }
            return Class.class;
    }

        };
        String []  columnaNombre={"Herramienta","Codigo","Marca","Total", "Disponibilidad", "Cantidad a Pedir", "Selección"};
        TableModel.setColumnIdentifiers(columnaNombre);
        Object[] fila=new Object[TableModel.getColumnCount()];
        for (int i = 0; i < marcas.size(); i++) {
           fila[0]=marcas.get(i).getHerramienta().getNombre();
           fila[1]=marcas.get(i).getHerramienta().getCodigo();
           fila[2]=marcas.get(i).getNombre();
           fila[3]=marcas.get(i).getCantidad();
           fila[4]=marcas.get(i).getDisponible();
           fila[5]="";
         TableModel.addRow(fila);
        }
      tablaHerramientas.setModel(TableModel);
      //tablaHerramientas.setRowSelectionAllowed(false);
     TableModel=null;
     }
    /**
     * @param args the command line arguments
     */
 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox OTSh;
    private javax.swing.JButton btnAceptarH;
    private javax.swing.JButton btnBuscarH;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnElimnarPH;
    private javax.swing.JButton btnOkH;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lblImagen;
    private javax.swing.JLabel lblOT1;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JTable listaPedidosH;
    private javax.swing.JTable tablaHerramientas;
    private javax.swing.JTextField txtNomHerramienta;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

 public List<OrdenTrabajo> getOrdenTrabajos() {
        return ordenTrabajos;
    }

    public void setOrdenTrabajos(List<OrdenTrabajo> ordenTrabajos) {
        this.ordenTrabajos = ordenTrabajos;
    }


}
